package com.paipai.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.paipai.dao.BiddingMapper;
import com.paipai.dao.ProductMapper;
import com.paipai.entity.Bidding;
import com.paipai.entity.Product;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;


@Transactional
@Service
public class BiddingService {
	@Autowired
	private BiddingMapper biddingMapper;
	
	@Autowired
	private ProductMapper productMapper;
	
	
	@Autowired
	private StringRedisTemplate redistemplate;
	

	public BiddingMapper getBiddingMapper() {
		return biddingMapper;
	}

	private static final String KEY_PRICE = "paipai:product:price";

	@Autowired
	private StringRedisTemplate redisTemplate;
	
	
	/**
	 * 查询商品 竞价信息
	 * @param id
	 * @return
	 */
	public List<Bidding> ProductBidding(Integer id) {
		
		List<Bidding> list = biddingMapper.findBiddingByProId(id);
		return list;
	}


	/**
	 * 用户出价
	 * @param id
	 * @param offerPrice
	 * @param uid 
	 * @param uname 
	 * @return
	 */

	public String executeBidding(Integer proId, Float offerPrice, String uname, Integer uid) {
		if(proId==null||proId==0||offerPrice==null ||offerPrice==0) return "no";
		//以id 常量为锁
		synchronized (proId.toString().intern()) {
			//以id查询商品当前价格以及最小加价
			Product product= productMapper.findProductPresentPriceAndMinimumPriceById(proId);
			
			if(product==null) return "no";
			
			
			
			//获取商品到期时间
			Date deadline = product.getDeadline();
			long endDate = deadline.getTime();
			
			//当前时间戳
			long TimeMillis = System.currentTimeMillis();
			
			//如果时间已到期
			if(endDate-TimeMillis<=0) return "expire";
			
			//最小加价
			Float minimumPrice = product.getMinimumPrice();
			
			//当前价格
			Float presentPrice = product.getPresentPrice();
			
			//如果出价小于(当前价格加最小价格 返回false)
			if(offerPrice<(minimumPrice+presentPrice)) return "priceNot";
			
			//将用户出价设置为当前价格
			product.setPresentPrice(offerPrice);
			
			//竞价次数加1
			product.setFrequency(product.getFrequency()+1);
			
			//根据主键id更新商品表  更新不会null值的
			int row = productMapper.updateByPrimaryKeySelective(product);
			
			//商品表更新失败 返回false
			if(row==0) return "no";
			
			//删除redis商品表
			HashOperations<String, Object, Object> hash = redistemplate.opsForHash();
			
			hash.delete("product", product.getId().toString());
			
			//加入一条redis价格表
			hash.put(KEY_PRICE,product.getId().toString(), offerPrice.toString());
			
			//插入一条竞价表信息
			Bidding bid = new Bidding();
			
			//设置商品id
			bid.setProId(product.getId());
			bid.setProName(product.getProName());
			//图片
			bid.setImg(product.getImg());
			//设置用户id
			bid.setUserId(uid);
			bid.setUserName(uname);
			//设置商家id
			bid.setSellerId(product.getSellerId());
			bid.setSellerName(product.getSellerName());
			//设置当前价格 以及 出价时间
			bid.setThisPrice(offerPrice);
			bid.setBidDate(new Date());
			//设置结束时间
			bid.setDeadline(product.getDeadline());
			
			
			//插入
			int row2 = biddingMapper.insert(bid);
			//成功/失败
			return row2>0? "ok":"no";
			
			
		}
		
	}



	/**
	 * 用户查询自己参与的竞价记录
	 * @param uid
	 * @return
	 */
	public List<List<Bidding>> findUserbiddingrecord(Integer uid) {
		List<List<Bidding>> Biddinglist = new ArrayList<List<Bidding>>();
		
		List<Bidding>list = biddingMapper.findUserbiddingrecord(uid);
		
		//定义一个分类id
		Integer proSort=0;
		
		for (Bidding bidding : list) {
			//如果 商品id 不等于  商品分类id
			if(bidding.getProId()!=proSort) {
				//就新建一个list
				ArrayList<Bidding> tempList = new ArrayList<Bidding>();
				tempList.add(bidding);
				//往分类list 添加该list
				Biddinglist.add(tempList);
				//再将商品id=分类id
				proSort=bidding.getProId();
			}else {
				//如果 商品id 等于  商品分类id 直接往分类list中的最后一个商品list加入该list
				//排序已经在数据库sql排列好
				Biddinglist.get(Biddinglist.size()-1).add(bidding);
			}
			
		}
		
		

		return Biddinglist;
	}

	/**
	 * 定时任务 查看商品是否有竞价记录
	 * @param id
	 * @return
	 */
	public Bidding findEndDatebiddingByProId(Integer id) {
		return biddingMapper.findEndDatebiddingByProId(id);
	}

}